package com.example.androiddemo;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentActivity;

import android.annotation.SuppressLint;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Random;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    private Random random;


    private List<Integer> chujikouData;
    //环境温度
    private Button ambientTemperature;
    private EditText ambientTemperatureEdit;

    //出机口温度
    private Button chujikouTemperature;
    private EditText chujikouTemperatureEdit;

    //入仓温度
    private Button rucangTemperature;
    //浇筑温度
    private Button jiaozhuTemperature;

    //混凝土表面温度
    private Button hunningtuBiaomianTemperature;
    private EditText hunningtuBiaomianTemperatureEdit;

    //混凝土内部温度
    private Button hunningtuNeibBuTemperature;
    private EditText hunningtuNeibBuBeginTemperatureEdit;
    private EditText hunningtuNeibBuMaxTemperatureEdit;
    private EditText hunningtuNeibBuMinTemperatureEdit;


    private Button jinShuiTemperature;
    private EditText jinShuiTemperatureEdit;



    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        random = new Random();
        initView();
    }

    public void initView(){
        ambientTemperature = findViewById(R.id.ambient_temperature);
        ambientTemperatureEdit = findViewById(R.id.ambient_temperature_edittext);

        chujikouTemperature = findViewById(R.id.chujikou_temperature);
        chujikouTemperatureEdit = findViewById(R.id.chujikou_temperature_edittext);
        rucangTemperature = findViewById(R.id.rucang_temperature);
        jiaozhuTemperature = findViewById(R.id.jiaozhu_temperature);
        hunningtuBiaomianTemperature = findViewById(R.id.hunningtu_biaomian_temperature);
        hunningtuBiaomianTemperatureEdit = findViewById(R.id.hunningtu_biaomian_temperature_edittext);


        hunningtuNeibBuTemperature = findViewById(R.id.hunningtu_neibu_temperature);
        hunningtuNeibBuBeginTemperatureEdit = findViewById(R.id.hunningtu_neibu_begin_temperature_edittext);
        hunningtuNeibBuMaxTemperatureEdit = findViewById(R.id.hunningtu_neibu_max_temperature_edittext);
        hunningtuNeibBuMinTemperatureEdit = findViewById(R.id.hunningtu_neibu_min_temperature_edittext);

        jinShuiTemperature = findViewById(R.id.jinshui_temperature);
        jinShuiTemperatureEdit = findViewById(R.id.jinshui_temperature_edittext);

        ambientTemperature.setOnClickListener(this);
        chujikouTemperature.setOnClickListener(this);
        rucangTemperature.setOnClickListener(this);
        jiaozhuTemperature.setOnClickListener(this);
        hunningtuBiaomianTemperature.setOnClickListener(this);
        hunningtuNeibBuTemperature.setOnClickListener(this);
        jinShuiTemperature.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.ambient_temperature:
                String s = ambientTemperatureEdit.getText().toString();
                int num = 10;
                if (s.length() > 0){
                    num = Integer.valueOf(s);
                }
                mutilpAmbientTemperature(num,19,36);
                break;
            case R.id.chujikou_temperature:
                mutilpChujikouTemperature();
                break;
            case R.id.rucang_temperature:
                mutilpTemperature(Type.RUCANG);
                break;
            case R.id.jiaozhu_temperature:
                mutilpTemperature(Type.JIAOZHU);
                break;
            case R.id.hunningtu_biaomian_temperature:
                mutilpHunningTuBiaoMianTemperature();
                break;
            case R.id.hunningtu_neibu_temperature:
                mutilpHunningTuNeiBuTemperature();
                break;
            case R.id.jinshui_temperature:
                String jinshuiString = jinShuiTemperatureEdit.getText().toString();
                int jinshuiNum = 10;
                if (jinshuiString.length() > 0){
                    jinshuiNum = Integer.valueOf(jinshuiString);
                }
                mutilpAmbientTemperature(jinshuiNum,10, 12);
                break;

        }
    }

    //生成环境温度/进水温度
    private void mutilpAmbientTemperature(int num,int min,int max) {

        String res = "";
        List<Integer> integers = mutilpRandom(min, max, num);
        for (int i = 0; i < integers.size(); i++) {
            res += integers.get(i) + "\n";
        }
        copyTextToClipboard(res);
    }

    //生成出机口温度
    private void mutilpChujikouTemperature() {
        String s = chujikouTemperatureEdit.getText().toString();
        int num = 10;
        if (s.length() > 0){
            num = Integer.valueOf(s);
        }
        String res = "";
        chujikouData = mutilpRandom(18, 21, num);
        for (int i = 0; i < chujikouData.size(); i++) {
            res += chujikouData.get(i) + "\n";
        }
        copyTextToClipboard(res);
    }

    //生成入仓/浇筑温度
    private void mutilpTemperature(int type) {

        if (chujikouData == null){
            Toast.makeText(this, "请先计算出机口温度", Toast.LENGTH_SHORT).show();
            return;
        }

        String res = "";
        for (int i = 0; i < chujikouData.size(); i++) {
            if (type == Type.RUCANG){
                res += (chujikouData.get(i) + 0.5) + "\n";
            }
            if (type == Type.JIAOZHU){
                res += (chujikouData.get(i) + 1) + "\n";
            }
        }
        copyTextToClipboard(res);
    }

    //生成混凝土表面温度
    private void mutilpHunningTuBiaoMianTemperature() {
        String s = hunningtuBiaomianTemperatureEdit.getText().toString();
        int num = 10;
        if (s.length() > 0){
            num = Integer.valueOf(s);
        }
        String res = "";
        List<String> data = mutilpRandomDouble(18, 23, num);
        for (int i = 0; i < data.size(); i++) {
            res += data.get(i) + "\n";
        }
        copyTextToClipboard(res);
    }

    //生成混凝土内部温度
    private void mutilpHunningTuNeiBuTemperature() {
        String begin = hunningtuNeibBuBeginTemperatureEdit.getText().toString();
        String max = hunningtuNeibBuMaxTemperatureEdit.getText().toString();
        String min = hunningtuNeibBuMinTemperatureEdit.getText().toString();
        int beginNum = 30;
        int maxNum = 49;
        int minNum = 25;
        if (begin.length() > 0){
            beginNum = Integer.valueOf(begin);
        }
        if (max.length() > 0){
            maxNum = Integer.valueOf(max);
        }
        if (min.length() > 0){
            minNum = Integer.valueOf(min);
        }
        String res = "";
        List<String> dataFirst = mutilpRandomDoubleSort(beginNum, maxNum, 10,false);
        List<String> dataLast = mutilpRandomDoubleSort(minNum, maxNum, 24,true);
        for (int i = 0; i < dataFirst.size(); i++) {
            res += dataFirst.get(i) + "\n";
        }

        for (int i = 0; i < dataLast.size(); i++) {
            res += dataLast.get(i) + "\n";
        }

        copyTextToClipboard(res);
    }

    public List<Integer> mutilpRandom(int min, int max,int num){
        List<Integer> temp = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            temp.add(min + random.nextInt(max + 1 - min));
        }
        return temp;
    }

    //isLarge true  表示从大到小
    public List<String> mutilpRandomDoubleSort(int min, int max, int num, boolean isLarge){
        List<String> tempString = new ArrayList<>();
        DecimalFormat df = new DecimalFormat("#.0");
        List<Double> tempDouble = new ArrayList<>();

        for (int i = 0; i < num; i++) {
            double res = min + Math.random() * (max - min);
            tempDouble.add(res);
        }
        if (isLarge){
            Collections.sort(tempDouble, new Comparator<Double>() {
                @Override
                public int compare(Double o1, Double o2) {
                    return o2.compareTo(o1);
                }
            });
        }else {
            Collections.sort(tempDouble);
        }

        for (int i = 0; i < tempDouble.size(); i++) {
            tempString.add(df.format(tempDouble.get(i)));
        }
        return tempString;
    }

    public List<String> mutilpRandomDouble(int min, int max,int num){
        List<String> temp = new ArrayList<>();
        DecimalFormat df = new DecimalFormat("#.0");

        for (int i = 0; i < num; i++) {
            double res = min + Math.random() * (max - min);
            String formattedDecimal = df.format(res);
            temp.add(formattedDecimal);
        }
        return temp;
    }


    public void copyTextToClipboard(String text) {
        ClipboardManager clipboard = (ClipboardManager) this.getSystemService(Context.CLIPBOARD_SERVICE);
        clipboard.setPrimaryClip(ClipData.newPlainText("label", text));
        Toast.makeText(this, "文本已复制到剪贴板", Toast.LENGTH_SHORT).show();
    }

    public interface Type{
        int RUCANG = 0;
        int JIAOZHU = 1;
    }
}